package com.trevor.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.trevor.domain.User;
import com.trevor.form.UserForm;
import com.trevor.services.LoginService;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm=(UserForm) form;
		User user=new User();
		user.setUserId(userForm.getUserId());
		user.setUserPassword(userForm.getUserPassword());
		LoginService ls=new LoginService();
		user=ls.checkUser(user);
		//user.setCourses(ls.getCourses(user));
		if(user!=null){
			user.setCourses(ls.getCourses(user));
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("courses", ls.courseToCn(user.getCourses()));
			System.out.println(request.getSession().getId());
			return mapping.findForward("loginok");
		}else{
			return mapping.findForward("loginfail");
		}
	}
	
}
